package com.wyw.function.learning.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Title
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WordCounter {
    private int counter;
    private boolean lastSpace;

    // 迭代方法
    public WordCounter accumulate(Character c) {
        if (Character.isWhitespace(c)) {
            return lastSpace ? this: new WordCounter(counter, true);
        } else {
            // 上一个字符式空格，而当前遍历的字符不是空格，单词计数器+1
            return lastSpace ? new WordCounter(counter + 1, false) : this;
        }
    }

    // 合并两个WordCounter
    public WordCounter combine(WordCounter wordCounter) {
        // 只关心总和
        return new WordCounter(counter + wordCounter.getCounter(), wordCounter.lastSpace);
    }

    public int getCounter() {
        return counter;
    }
}

